﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace docmgr.WinForm
{
    public partial class AddReadPeople : Form
    {
        String condition = "";
        public DataTable userCheck;
        public  DataTable docDatable = null;
        docmgr.BLL.Department departmentBll = new BLL.Department();
        public AddReadPeople()
        {
            InitializeComponent();
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel, ((byte)(134)));

            
        }

        public AddReadPeople(DataTable dt)
        {
            this.docDatable = dt;
            InitializeComponent();
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel, ((byte)(134)));

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            //添加传阅
            ArrayList list = new ArrayList();

            for(int i=0;i<dataGridView1.RowCount;i++)
            {
                DataGridViewCheckBoxCell checkcell = (DataGridViewCheckBoxCell)dataGridView1.Rows[i].Cells["checkbox"];
 
                Boolean flag = Convert.ToBoolean(checkcell.Value);

                if(flag)
                {
                    list.Add(Convert.ToInt32(dataGridView1.Rows[i].Cells["id"].Value));
                }
            }
            if(list.Count==0)
            {
                MessageBox.Show("没有选中人员，无法添加");
                return;
            }

            String sqlStr = "insert [DocReadLog] ([DocumentID],[LeaderId],[StartDate],[Status],[ReadUserID]) Values";

            for(int i=0;i<list.Count;i++)
            {
                if (i != 0)
                    sqlStr += " , ";
                sqlStr += String.Format("({0},{1},'{2}','未读',{3} )", Convert.ToInt32(docDatable.Rows[0]["ID"]),Convert.ToInt32(Common.curUser.Rows[0]["ID"]),DateTime.Now.ToString(),list[i]);
            }

            SQLHelper.ExecuteSql(SQLHelper.connString, sqlStr);
            this.Close();

        }

        void bindData()
        {
            String sqlStr = "SELECT u.id,u.name 姓名,d.name 部门名称 from [user] as u left join Department as d on  u.departmentid=d.id";
            if(condition!="")
            {
                sqlStr += " where  "+ condition ;
            }
            userCheck = SQLHelper.ExecuteDataSet(SQLHelper.connString, sqlStr).Tables[0];
            dataGridView1.DataSource = userCheck;
            dataGridView1.Refresh();
        }

        private void AddReadPeople_Load(object sender, EventArgs e)
        {
            String sqlStr = "SELECT u.id,u.name 姓名,d.name 部门名称 from [user] as u left join Department as d on  u.departmentid=d.id";

            userCheck = SQLHelper.ExecuteDataSet(SQLHelper.connString, sqlStr).Tables[0];
            dataGridView1.DataSource = userCheck;


            DataGridViewCheckBoxColumn column1 = new DataGridViewCheckBoxColumn();
            {
                column1.HeaderText = "选择";
                column1.Name = "checkbox";
                column1.AutoSizeMode =
                DataGridViewAutoSizeColumnMode.DisplayedCells;
                column1.FlatStyle = FlatStyle.Standard;
            }
            dataGridView1.Columns.Add(column1); 

            initDepcomboBox();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            condition = "u.DepartmentID = " + DepcomboBox.SelectedValue;
            bindData();
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            condition = "";
            bindData();
        }

        void initDepcomboBox()
        {
            DataSet xx = new DataSet();
            xx.Tables.Clear();
            xx.Tables.Add(departmentBll.GetList("1=1"));
            this.DepcomboBox.DataSource = xx.Tables[0];
            this.DepcomboBox.DisplayMember = "Name";
            this.DepcomboBox.ValueMember = "ID";
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex > -1 && e.RowIndex > -1)
            {
                if (Convert.ToBoolean(dataGridView1.Rows[e.RowIndex].Cells["checkbox"].Value) != true)
                    dataGridView1.Rows[e.RowIndex].Cells["checkbox"].Value = true;
                else
                    dataGridView1.Rows[e.RowIndex].Cells["checkbox"].Value = false;

            }
        }
    }
}
